<?php

/**
 * 自动生成 WxEventAwardController
 * 
 * Apps/Mp/Controller/WxEventAwardController.class.php
 * cmcc_admin @ 2015-07-06
 */

namespace Mp\Controller;

use Common\Service\WxEventAwardService;

class WxEventTurntableController extends MpController {

  public function index() {
    $action = I('get.action', 'read');
  
    $data = $this->_init_data();
    $data['module_name'] = '拆礼盒 - 微信功能';
    $data['module_url'] = U( $this->base_path . 'index');
  
    $WxEventOpenbox = WxEventOpenboxService::instance();
    $data['config'] = array(
        'action' => $action,
        'action_label' => $this->action_label[$action],
        'menu_id'    => 'menu_Mp_MerWxSetting_index' ,
        'module_url' => U($this->base_path . 'index'),
        'read_url'   => U($this->base_path . 'read' ),
        'create_url' => U($this->base_path . 'create'),
        'update_url' => U($this->base_path . 'update'),
        'delete_url' => U($this->base_path . 'delete'),
        'upload_icon_url' => U($this->base_path . 'upload') .'/icon',
        'del_icon_url' => U($this->base_path . 'delete_icon') ,
        'wx_event_user_url' => U( 'Mp/WxEventUser/index' ) . '/'. $WxEventOpenbox->type .'/'  ,
        'wx_event_award_url' => U( 'Mp/WxEventAward/index' ) . '/'. $WxEventOpenbox->type .'/' ,
        'page' => 1,
        'page_size' => C('PAGE_SIZE'),
        'row' => $WxEventOpenbox->get_default_row()
    );
  
    $data['param'] = array();
  
    $this->display_with_layout($data, $this->controller .':index');
  }
  
  function read() {
    $post = I('post.');
  
    $config = array(
        'page' => $post['page'],
        'page_size' => $post['page_size'],
        'sort' => empty($post['sort']) ? 'id' : $post['sort'],
        'order' => empty($post['order']) ? 'DESC' : $post['order'],
    );
  
    $WxEventOpenbox = WxEventOpenboxService::instance();
    $data['rows'] = $WxEventOpenbox->get_by_cond($config);
  
    $config['count'] = TRUE;
    $data['total'] = $WxEventOpenbox->get_by_cond($config);
  
    $this->ajaxReturn($data);
  }
  
  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
  
    $post['mer_id'] = $this->mer_id;
    $WxEventOpenbox = WxEventOpenboxService::instance();
    $row = $WxEventOpenbox->get_default_row();
  
    if ( $post['icon'] ==  $row['icon'] ) {
      $post['icon'] = $this->_save_new_filename($post['icon']);
    }
  
    if ( $post['home_bg_img'] ==  $row['home_bg_img'] ) {
      $post['home_bg_img'] = $this->_save_new_filename($post['home_bg_img']);
    }
  
    if ( $post['box_bg_img'] ==  $row['box_bg_img'] ) {
      $post['box_bg_img'] = $this->_save_new_filename($post['box_bg_img']);
    }
  
    if ( $post['award_adv_img'] ==  $row['award_adv_img'] ) {
      $post['award_adv_img'] = $this->_save_new_filename($post['award_adv_img']);
    }
  
    if ( $post['end_icon'] ==  $row['end_icon'] ) {
      $post['end_icon'] = $this->_save_new_filename($post['end_icon']);
    }
  
    $ret = $WxEventOpenbox->create($post);
    $this->ajaxReturn($ret);
  }
  
  function _save_new_filename( $filename ) {
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    $path = 'Public/upload/icon/' . date('Ym');
    if ( !file_exists($path) ) {
      mkdir($path, 0777);
    }
  
    $new_filename = $path . '/' . rand_string(13) .'.' . $ext;
    copy($filename, $new_filename);
  }
  
  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
  
    $WxEventOpenbox = WxEventOpenboxService::instance();
    $ret = $WxEventOpenbox->update($id, $post);
    $this->ajaxReturn($ret);
  }
  
  function delete() {
    $ids = I('post.ids');
    $WxEventOpenbox = WxEventOpenboxService::instance();
    $ret = $WxEventOpenbox->delete($ids);
    $this->ajaxReturn($ret);
  }
}
